package cn.jiyun.controller;

import cn.jiyun.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RequestMapping("xiaofeizhe")
@Controller
public class EmployeeController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired(required = false)
    private DiscoveryClient discoveryClient;
    @RequestMapping("diaoyongTigongzhe")
    @ResponseBody
    public Employee getAll(){
        //根据服务名称，获取服务实例
        List<ServiceInstance> tigongzhe = discoveryClient.getInstances("tigongzhe");
        //因为只有一个tigongzhe，所以获取第一个
        ServiceInstance serviceInstance = tigongzhe.get(0);
        //获取ip和端口信息，拼接成服务地址
        String baseUrl = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/tigongzhe/getAll";
        Employee forObject = restTemplate.getForObject("http://localhost:8080/tigongzhe/getAll", Employee.class);
        return forObject;
    }
}
